/**
 * 在屏幕上放置一个元素，在1s内点击3次，显示 vConsole
 *
 * @param config
 */
function panxVConsole(config) {
  var TIME = +new Date;
  var VC_CDN = "https://res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/vconsole/3.0.0/vconsole.min.js?v" + TIME;
  var VC_INSTANCE = null;

  function injectTrigger() {
    var triggerElem = document.createElement('div');
    triggerElem.classList.add('panx-vconsole-trigger')
    triggerElem.style.height = "30px";
    triggerElem.style.width = "30px";
    triggerElem.style.zIndex = "100000";
    triggerElem.style.position = "fixed";
    triggerElem.style.backgroundColor = config.bgColor || 'transparent';
    triggerElem.style.opacity = '0.5';
    triggerElem.dataset.clickCount = '0';
    if (config.left) triggerElem.style.left = config.left;
    if (config.right) triggerElem.style.right = config.right;
    if (config.top) triggerElem.style.top = config.top;
    if (config.bottom) triggerElem.style.bottom = config.bottom;
    triggerElem.onclick = triggerAppendVconsole;
    triggerElem.ontouchstart = triggerAppendVconsole;
    document.body.appendChild(triggerElem);

    function triggerAppendVconsole(event) {
      if(Number(this.dataset.clickCount++) === 2 && ! VC_INSTANCE) {
        injectVConsole();
      }
      // should click 3 times in 1000ms
      setTimeout(function () {
        triggerElem.dataset.clickCount = '0';
      }, 1000);
      event.stopPropagation();
    }
  }

  function injectVConsole(){
    var consoleScript = document.createElement('script');
    consoleScript.src = VC_CDN;
    document.head.appendChild(consoleScript);
    consoleScript.onload = function() {
      console.info("init vConsole");
      VC_INSTANCE = new VConsole()
    };
    consoleScript.onerror = function() {
      console.info("failed load vConsole");
    }
  }

  injectTrigger();
}